package sd.czw.com.mmgapp.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.zhy.http.okhttp.OkHttpUtils;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.customviews.ChildViewPager;
import sd.czw.com.mmgapp.mcallback.CategeroyTitleCallback;
import sd.czw.com.mmgapp.model.categorybean.CategoryTitleInfo;

/**
 * Created by czw
 * desc 描述：分类界面
 * on 2017/7/5.
 */
public class CategeroyFragment extends Fragment {


    private View rootviews;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private List<CategoryTitleInfo.ErrmsgBean> datas = new ArrayList<>();
    private TabLayout tab;
    private ChildViewPager vp;
    private List<String> path = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootviews = inflater.inflate(R.layout.fragment_categeroy, container, false);
        tab = (TabLayout)rootviews.findViewById(R.id.tab);
        vp = (ChildViewPager) rootviews.findViewById(R.id.vp);
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/top01.jpg");
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/top02.jpg");
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/top03.jpg");
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/top04.jpg");
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/top05.jpg");
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/top06.jpg");
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/top07.jpg");
        path.add("http://miemiegou.com/wap/themes/miqinew/images/yimage/new_banner.jpg");
        getdatas();
        return rootviews;
    }

    private void getdatas() {
        OkHttpUtils.post().url(Contants.FL_LIST).addParams("time",Contants.Time)
                .addParams("sign",Contants.Sign).addParams("act","cat").build()
                .execute(new CategeroyTitleCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(CategoryTitleInfo response, int id) {
                        if (response.getErrcode() == 1){
                            List<CategoryTitleInfo.ErrmsgBean> errmsg = response.getErrmsg();
                            datas.addAll(errmsg);
                            getTitleFragment();
                            MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager(),fragments,titles);
                            vp.setAdapter(myViewPagerAdapter);
                            myViewPagerAdapter.notifyDataSetChanged();
                            tab.setupWithViewPager(vp);
                        }
                    }
                });
    }

    private void getTitleFragment() {
        for (int i = 0; i < path.size();i++){
            titles.add(datas.get(i).getName());
            tab.addTab(tab.newTab().setText(titles.get(i)));
            fragments.add(TabGoodsFragment.newInstance(datas.get(i).getId()+"",path.get(i)));
        }
    }

    class MyViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;
        private List<String> titles;
        public MyViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}
